﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using ChanExt;
using C.Unite;
using C.Entities;
using MySoft.Data;
using System.Text;

namespace ArtLand.OA.Project.SystemMgr
{
    public partial class SysUserChange : CommonPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindGroup();
                LoadData();
            }
        }

        #region 绑定用户组
        public void BindGroup()
        {
            List<Sys_UserGroup> items = TbsDbSession.Default.From<Sys_UserGroup>().ToList();
            txt_GroupID.DataSource = items;
            txt_GroupID.DataTextField = "Groupname";
            txt_GroupID.DataValueField = "Ugid";
            txt_GroupID.DataBind();
        }
        #endregion


        private void LoadData()
        {

            // 关闭按钮的客户端脚本：
            // 1. 首先确认窗体中表单是否被修改（如果已经被修改，则弹出是否关闭的确认对话框）
            // 2. 然后关闭本窗体，回发父窗体
            btnClose.OnClientClick = ActiveWindow.GetConfirmHidePostBackReference();
            int id = 0;
            if (!string.IsNullOrEmpty(Request["id"]))
            {
                id = int.Parse(Request["id"]);
            }
            if (id > 0)
            {
                txt_eName.Enabled = false;
                Sys_Users tug = TbsDbSession.Default.From<Sys_Users>().Where(Sys_Users._.UserID == id).ToFirst();
                txt_eName.Text = tug.UserName;
                txt_TrueName.Text = tug.TrueName;
                txt_IsLock.Checked = tug.IsLock.Value;
                //if (tug.Mtype.HasValue)
                //    txt_Mtype.SelectedValue = tug.Mtype.Value.ToString();
                txt_GroupID.SelectedValue = tug.NewGroupID.ToString();
            }
        }



        protected void btnSaveRefresh_Click(object sender, EventArgs e)
        {
            int returnvalue = 0;
            Sys_Users tm = new Sys_Users();
            if (!string.IsNullOrEmpty(Request["id"]))
            {
                tm.UserID = int.Parse(Request["id"]);
                tm.Attach();
                if (txt_ePW.Text != "")
                {
                    //ChangePwd(CookInfo.EAdminErpUserID().ToString(), txt_ePW.Text);
                    tm.Pwd = StringHelper.md5(txt_ePW.Text, 16);
                }
                tm.NewGroupID = short.Parse(txt_GroupID.SelectedValue);
                tm.IsLock = txt_IsLock.Checked;
                tm.TrueName = txt_TrueName.Text;
               // tm.Mtype = int.Parse(txt_Mtype.SelectedValue);
                returnvalue = TbsDbSession.Default.Update<Sys_Users>(tm);
                // TbsDbSession.Default.FromSql("update Sys_Users set IsLock='" + txt_IsLock.Checked + "' where id=" + int.Parse(Request["id"])).ExecuteNonQuery();

            }
            else
            {
                tm.DeAttach();
                tm.AddTime = DateTime.Now;
                tm.UserName = txt_eName.Text;
                tm.Pwd= StringHelper.md5(txt_ePW.Text, 16);
                tm.NewGroupID = short.Parse(txt_GroupID.SelectedValue);
                tm.IsLock = txt_IsLock.Checked;
                tm.TrueName = txt_TrueName.Text;
                tm.Logins = 0;
               // tm.Mtype = int.Parse(txt_Mtype.SelectedValue);
                returnvalue = TbsDbSession.Default.Insert<Sys_Users>(tm);
            }
            if (returnvalue > 0)
            {
                PageContext.RegisterStartupScript(ActiveWindow.GetHideRefreshReference());
            }
        }





    }
}